﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Web.Security;

namespace ProjectSem40701.Admin
{
    public partial class ArticleEdit : System.Web.UI.Page
    {
        /// <summary>
        /// Declare member variables here
        /// </summary>
        int ArticleID;
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                ArticleID = Convert.ToInt32(Request.QueryString["ArticleID"].ToString().Trim());
                AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter dp = new AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter();
                DataTable dttbl = dp.Get_tbl_Article_Select_All_By_ArticleID(ArticleID);
                this.txtArticleTitle.Text = dttbl.Rows[0]["ArticleTitle"].ToString();
                this.txtDescription.Value = Server.HtmlDecode(dttbl.Rows[0]["ArticleDescription"].ToString());
                this.txtArticleDateCreated.Text = Convert.ToDateTime(dttbl.Rows[0]["ArticleDateCreated"].ToString()).ToString("dd/MM/yyyy");
                this.txtContent.Value = Server.HtmlDecode(dttbl.Rows[0]["ArticleContent"].ToString());

                foreach (ListItem item in this.cbbArticleType.Items)
                {
                    if (item.Value == dttbl.Rows[0]["ArticleTypeID"].ToString())
                    {
                        item.Selected = true;
                    }
                }

                foreach (ListItem item in this.cbbArticleStatus.Items)
                {
                    if (item.Value == dttbl.Rows[0]["ArticleStatus"].ToString())
                    {
                        item.Selected = true;
                    }
                }

                this.imgArticleImage.ImageUrl = "~/images/" + dttbl.Rows[0]["ArticleImage"].ToString();
                this.lblDateCreated.Text = dttbl.Rows[0]["ArticleDateCreated"].ToString();
                this.lblUserCreated.Text = dttbl.Rows[0]["UserName"].ToString();

            }
            catch
            {
                //bao loi xay ra
            }
        }

        /// <summary>
        /// Khi người dùng ấn nút update
        /// </summary>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ArticleID = Convert.ToInt32(Request.QueryString["ArticleID"].ToString().Trim());

                string strUser = System.Web.HttpContext.Current.User.Identity.Name;
                string strUserID = Membership.GetUser(strUser).ProviderUserKey.ToString();
                Guid userID = new Guid(strUserID);

                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
                DateTime dtCreatedDate = Convert.ToDateTime(this.txtArticleDateCreated.Text.Trim());

                string strImageSource;

                if (this.fileUpArticleImage.HasFile)
                {
                    string str = DateTime.Now.ToString("ddMMyyyyhhmmssms");
                    this.fileUpArticleImage.SaveAs(Server.MapPath("~/images/" + str +
                        this.fileUpArticleImage.FileName.Trim()));
                    strImageSource = str + this.fileUpArticleImage.FileName.Trim();
                }
                else
                {
                    strImageSource = this.imgArticleImage.ImageUrl.Replace("~/images/", "");
                }


                AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter dp = new AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter();

                int ok = dp.tbl_Article_Update(userID, Convert.ToInt32(this.cbbArticleType.SelectedValue.Trim()), dtCreatedDate, 
                    this.txtArticleTitle.Text.Trim(), strImageSource, 
                    Server.HtmlEncode(this.txtDescription.Value.Trim()), Server.HtmlEncode(this.txtContent.Value.Trim()), 
                    Convert.ToInt32(this.cbbArticleStatus.SelectedValue.Trim()), ArticleID);
                            

                if (ok > 0)
                {
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                Show_Dialog(FAIL);
            }
        }

        /// <summary>
        /// Khi người dùng ấn nút delete
        /// </summary>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ArticleID = Convert.ToInt32(Request.QueryString["ArticleID"].ToString().Trim());

                AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter dp = new AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter();
                int ok = dp.tbl_Article_Delete(ArticleID);

                if (ok > 0)
                {
                    Show_Dialog_Del(SUCCESS);
                }
                else
                {
                    Show_Dialog_Del(FAIL);
                }
            }
            catch
            {
                Show_Dialog_Del(FAIL);
            }
        }
        /// <summary>
        /// Đóng modal popup
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                        Response.Redirect("Article.aspx");
                    break;
                case FAIL:
                        this.btnUpdate_ModalPopupExtender.Enabled = false;
                        this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
                default:
                        this.btnUpdate_ModalPopupExtender.Enabled = false;
                        this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// show dialog
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                        this.ltrMessage.Text = "Cập nhật thành công";
                    break;
                case FAIL:
                        this.ltrMessage.Text = "Cập nhật thất bại";
                    break;
                default:
                        this.ltrMessage.Text = "Lỗi không xác định, cập nhật thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnUpdate_ModalPopupExtender.Enabled = true;
            this.btnUpdate_ModalPopupExtender.Show();
        }

        /// <summary>
        /// show dialog neu an xoa
        /// </summary>
        private void Show_Dialog_Del(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                        this.ltrMessage.Text = "Xóa thành công";
                    break;
                case FAIL:
                        this.ltrMessage.Text = "Xóa thất bại";
                    break;
                default:
                        this.ltrMessage.Text = "Lỗi không xác định, xóa thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnDelete_ModalPopupExtender.Enabled = true;
            this.btnDelete_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
